package com.ym.manager.admin.complain;

import com.alibaba.fastjson.JSONObject;
import com.ym.datasource.DataSourceType;
import com.ym.datasource.UseDataSource;
import com.ym.datasource.redis.RedisOperate;
import com.ym.manager.admin.complain.vo.ComplainListVo;
import com.ym.utils.HttpUtils;
import com.ym.utils.MapHandler;
import com.ym.utils.Pages;
import com.ym.utils.StringHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: dhf
 * @ClassName: ComplainService
 * @Date: 2022/9/23 20:57
 * @Description:
 * @Version: 1.0
 */
@Service
public class ComplainService {
    @Autowired
    private RedisOperate redisOperate;
    @Autowired
    private ComplainDao complainDao;

    @UseDataSource(DataSourceType.YS_IM)
    public Pages<Map<String, Object>> getList(ComplainListVo complainListVo,int pageNum,int limit) {
        int begin = pageNum;
        complainListVo.dataBegin = begin;
        complainListVo.pageSize = limit;
        int count  = complainDao.getListCount(complainListVo);
        Pages<Map<String,Object>> pages = new Pages<>(count,pageNum==0?1:pageNum/limit,limit);
        List<Map<String,Object>> list = complainDao.getList(complainListVo);
        pages.setList(list);
        return pages;
    }

    @UseDataSource(DataSourceType.YS_IM)
    public int complainOpt(String id, int status) {
        String url = "";
        if(status==3){
            url = "http://172.24.194.56:8082/manager/complain/dealComplain";
        }
        if(status==4){
            url = "http://172.24.194.56:8082/manager/complain/freeze";
        }
        if(status==5){
            url = "http://172.24.194.56:8082/manager/complain/unfreeze";
        }
        String data = HttpUtils.post(url, MapHandler.initMap("id",id));
        JSONObject jsonObject = JSONObject.parseObject(data);
        if(!jsonObject.getString("code").equals("0")){
            throw new RuntimeException(jsonObject.getString("msg"));
        }else{
            if(status==4) {
                Long obj_id = complainDao.getObjid(StringHandler.getLong(id));
                if (StringHandler.isNotEmpty(obj_id)) {
                    String userKey = "user:info:" + obj_id;
                    redisOperate.del(userKey);
                }
            }
            complainDao.updateStatus(StringHandler.getLong(id),status);
        }
        return 0;
    }

    @UseDataSource(DataSourceType.YS_IM)
    public int complainDel(long id) {
        return complainDao.complainDel(id);
    }
}
